package com.chen.springboot.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chen.springboot.common.Result;
import com.chen.springboot.mapper.mongoMapper.PlayersMapper;
import com.chen.springboot.service.impl.PlayersServiceImpl;
import com.chen.springboot.vo.Players;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author chenchao
 * @date 2022/7/2 23:17
 */

@RestController
@RequestMapping("/players")
@Api(value = "PlayerController",tags = "mogodb球员管理")
public class PlayersController {

    @Resource
    private PlayersMapper playersMapper;

    @Resource
    private PlayersServiceImpl playersServiceImpl;

    @Resource
    private MongoTemplate mongoTemplate;


    @RequestMapping("/findPage")
    @ApiOperation(value = "mongodb分页查询")
    public Result findPage(@RequestParam(defaultValue="1") Integer pageNum,
                           @RequestParam(defaultValue="10") Integer pageSize,
                           @RequestParam(defaultValue="") String country,
                           @RequestParam(defaultValue="") String displayName) {


//        Page<Players> playersServiceImplPage = playersServiceImpl.findPage(pageNum, pageSize,country,displayName);

        return  Result.success(playersServiceImpl.findPage2(pageNum, pageSize,country,displayName));
      /*  if(playersServiceImplPage.getTotalPages()!=0){
            return Result.success(playersServiceImplPage);
        }else{
            return  Result.success(playersServiceImpl.findPage(pageNum, pageSize,country,displayName));
        }*/

    }






    @PostMapping("/addBatch3")
    @ApiOperation(value = "爬虫")
    public Result addBatch3(@RequestBody String str)  {
        CopyOnWriteArrayList<Players> copyOnWriteArrayList = new CopyOnWriteArrayList<>();

        JSONArray jsonArray = JSONArray.parseArray(str);
        for (int i = 0; i < jsonArray.size(); i++) {
            Players players=new Players();
            JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
            String playerProfile = jsonObject.getString("playerProfile");
            String teamProfile = jsonObject.getString("teamProfile");
            JSONObject object = JSONObject.parseObject(playerProfile);
//            球队
            JSONObject object2 = JSONObject.parseObject(teamProfile);
//            球员头像
            String playerImg = object.getString("playerId");
//            球员名称
            String displayName = object.getString("displayName");
            String country = object.getString("country");//球员国籍
            String height = object.getString("height");//球员身高
            String weight = object.getString("weight");//球员体重
            String position = object.getString("position");//球员定位
            String displayAbbr = object2.getString("displayAbbr"); //球员所在球队
            players.setPlayerImg("https://res.nba.cn/media/img/players/head/260x190/"+playerImg+".png");
            players.setCountry(country);
            players.setDisplayName(displayName);
            players.setPosition(position);
            players.setDisplayAbbr(displayAbbr);
            players.setHeight(height);
            players.setWeight(weight);
            players.setId(i+1);
            copyOnWriteArrayList.add(players);
            playersMapper.insert(players);
        }


        return Result.success();
    }

}
